package com.itheima.listener;

import com.itheima.constant.MqConstants;
import com.itheima.service.HotelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class IndexListener {

    @Autowired
    private HotelService hotelService;

    /**
     * 监听Mq消息，并进行es索引库数据添加操作
     */
    @RabbitListener(queues = MqConstants.HOTEL_INSERT_QUEUE)
    public void insertListener(Long id) {
        //接收到消息,记录日志
        log.info("IndexListener insert message accept id:{} ...",id);
        //调用服务进行es索引库数据添加操作
        try {
            hotelService.insertById(id);
            //消费消息成功，记录日志
            log.info("IndexListener insert message success id:{}...",id);
        } catch (Exception e) {
            log.error("IndexListener insert error:{},id:{}",e.getMessage(),id);
        }


    }

    /**
     * 监听Mq消息，并进行es索引库数据删除操作
     */
    @RabbitListener(queues = MqConstants.HOTEL_DELETE_QUEUE)
    public void deleteListener(Long id) {
        //接收到消息,记录日志
        log.info("IndexListener delete message accept id:{} ...",id);
        //调用服务进行es索引库数据添加操作
        try {
            hotelService.deleteById(id);
            //消费消息成功，记录日志
            log.info("IndexListener delete message success id:{}...",id);
        } catch (Exception e) {
            log.error("IndexListener delete error:{},id:{}",e.getMessage(),id);
        }


    }

}
